package com.stargate.bd.dao.factory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;

import com.stargate.bd.bean.Etat;
import com.stargate.bd.bean.Sortie;
import com.stargate.bd.dao.DAO;

public class SortieDAO extends DAO<Sortie> {

	@Override
	public ArrayList<Sortie> selectAll() throws SQLException {
		String sql =  "SELECT * FROM " + Sortie.TABLE_NAME;
		ArrayList<Sortie> sorties = new ArrayList<Sortie>();
		Statement statement = connection.createStatement();
		ResultSet resultat = statement.executeQuery(sql);
		while (resultat.next()) {
			sorties.add(new Sortie(resultat.getInt(Sortie.NUM_ACCES),
						resultat.getString(Sortie.CODE_POINT_ACCES),
						resultat.getString(Sortie.CODE_BATIMENT),
						Etat.valueOf(resultat.getString(Sortie.ETAT_SORTIE)),
						resultat.getDate(Sortie.DATE_SORTIE)));
		}
		resultat.close();
		statement.close();
		return sorties;
	}

	@Override
	public Sortie select(Sortie bean) throws SQLException {
		ArrayList<Sortie> sorties = selectAll();
		int index = sorties.indexOf(bean);
		return index == -1 ? null : sorties.get(index);
	}

	@Override
	public void update(Sortie bean) throws SQLException {
		String sql = "UPDATE " + Sortie.TABLE_NAME +
				" SET " + Sortie.ETAT_SORTIE + "=?, " + Sortie.DATE_SORTIE + "=? " +
				"WHERE " + Sortie.NUM_ACCES + "=? " +
				"AND " + Sortie.CODE_POINT_ACCES + "=? " +
				"AND " + Sortie.CODE_BATIMENT + "=?";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getEtatSortie().getEtat(), Types.VARCHAR);
		statement.setObject(2, bean.getDateSortie(), Types.DATE);
		statement.setObject(3, bean.getNumAcces(), Types.INTEGER);
		statement.setObject(4, bean.getCodePointAcces(), Types.CHAR);
		statement.setObject(5, bean.getCodeBatiment(), Types.VARCHAR);
		statement.executeUpdate();
		statement.close();
	}

	@Override
	public void delete(Sortie bean) throws SQLException {
		String sql = "DELETE FROM " + Sortie.TABLE_NAME + 
				" WHERE " + Sortie.NUM_ACCES + "=? " +
				"AND " + Sortie.CODE_POINT_ACCES + "=? " +
				"AND " + Sortie.CODE_BATIMENT + "=?";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getNumAcces(), Types.INTEGER);
		statement.setObject(2, bean.getCodePointAcces(), Types.CHAR);
		statement.setObject(3, bean.getCodeBatiment(), Types.VARCHAR);
		statement.executeUpdate();
		statement.close();
	}

	public void delete(int numAcces, String codePointAcces, String codeBatiment) throws SQLException {
		delete(new Sortie(numAcces, codePointAcces, codeBatiment, null, null));
	}

	@Override
	public void insert(Sortie bean) throws SQLException {
		String sql = "INSERT INTO " + Sortie.TABLE_NAME + " VALUES (?,?,?,?,?)";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getNumAcces(), Types.INTEGER);
		statement.setObject(2, bean.getCodePointAcces(), Types.CHAR);
		statement.setObject(3, bean.getCodeBatiment(), Types.VARCHAR);
		statement.setObject(4, bean.getEtatSortie().getEtat(), Types.VARCHAR);
		statement.setObject(5, bean.getDateSortie(), Types.DATE);
		statement.executeUpdate();
		statement.close();
	}

}
